<?php

/**
 * 自动生成 SysRoleService
 * 
 * Apps/Common/Service/SysRoleService.class.php
 * admin @ 2015-06-18
 */

namespace Common\Service;

class SysRoleService {

  private static $instance;

  //1级最低 10级最高
  var $rank = array(
    1  => '1级' ,
    2  => '2级' ,
    3  => '3级' ,
    4  => '4级' ,
    5  => '5级' ,
    6  => '6级' ,
    7  => '7级' ,
    8  => '8级' ,
    9  => '9级' ,
    10 => '10级' ,
  );
  
  public static function instance() {
    if (self::$instance == null) {
      $instance = new SysRoleService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'sort' => '99',
      'sys_type' => 2 ,
      'name' => '',
      'desc' => '',
      'expand' => '',
      'status' => 1,
      'rank'   => 1 ,
    );
  }
  
  function get_by_user_id( $user_id , $group_concat_role = FALSE ) {
    $SysUserRole = M('SysUserRole');
    $SysUserRole->alias('sur');
    if ( $group_concat_role  ) {
      $SysUserRole->field( ' GROUP_CONCAT(sur.role_id) as role_id , max(sr.rank) as rank' );
    }
    $SysUserRole->join('sys_role sr on sr.id = sur.role_id');
    $data = $SysUserRole->where('sur.user_id = %d' , $user_id )->find();
    return $data ? $data : array();
  }
  
  function get_by_user_id_for_auth( $user_id ) {
    $SysUserRole = M('SysUserRole');
    $data = $SysUserRole
            ->alias('sur')
            ->field( ' GROUP_CONCAT( sur.role_id ) AS role_id , GROUP_CONCAT( sr.name ) AS role_name , MAX(sr.rank) AS role_rank' )
            ->join('sys_role sr on sr.id = sur.role_id')
            ->where('sur.user_id = %d' , $user_id )
            ->find();
    return $data ? $data : array();
  }
      
  function get_by_id($id) {
    $SysRole = M('SysRole');
    $data = $SysRole->find($id);
    return $data ? $data : array();
  }
  
  function get_by_cond($config) {
    $default = array(
      'mer_id' => 0 ,
      'rank' => 10 ,
      'sys_type' => 2 ,
      'status' => '',
    );

    $config = extend($config, $default);
    $where = array(
      'sr.id' => array('NEQ' , 0 )
    );
    
    if (!empty($config['status'])) {
      $where['sr.status'] = $config['status'];
    }
    
    if ( empty($config['mer_id']) ) {
      $where['sr.mer_id'] = 0 ;
    } else {
      $where['sr.mer_id'] = $config['mer_id'] ;
    }
    
    if ( !empty($config['sys_type']) ) {
      $where['sr.sys_type'] = $config['sys_type'];
    }
    
    if ( !empty($config['rank']) ) {
      $where['sr.rank'] = array( 'LT' , $config['rank'] );
    }
    
    $SysRole = M('SysRole');
    $order = ' sr.rank DESC , sr.sort ASC ';
    $data = $SysRole->alias('sr')
      ->field('sr.*, sd.text as sys_type_text ' )
      ->join('sys_Dictionary sd on sd.id = sr.sys_type' , 'left')
      ->where($where)->order($order)->select();
    
    return $data ? $data : array();
  }

  function create($data) {
    $SysRole = M('SysRole');
    $ret = $SysRole->add($data);
    if ($ret) {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    } else {
      return ajax_arr('添加失败', FALSE);
    }
  }

  function update($id, $data) {
    $SysRole = M('SysRole');

    $ret = $SysRole->where("id = %d", $id)->save($data);
    if ($ret) {
      return ajax_arr('编辑成功', TRUE);
    } else {
      return ajax_arr('编辑失败', FALSE);
    }
  }


  function delete($ids) {
    $SysRole = M('SysRole');
    
    $user_data = $this->get_user_role($ids);
    if ( $user_data ) {
      return ajax_arr('还有用户是该角色，请先将用户转移到其它角色');
    }
    
    $ret = $SysRole->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }
  
  function delete_user_role_by_user_id( $user_id ) {
    $SysUserRole = M('SysUserRole');
    $ret = $SysUserRole->where( 'user_id = %d' , $user_id )->delete();
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }

  function get_user_role( $role_id ) {
    $SysUserRole = M('SysUserRole');
    $data = $SysUserRole->where( 'role_id = %d' , $role_id )->find();
    return $data ? $data : array();
  }
  
  function create_user_role( $user_id , $role_id_arr ) {
    $SysUserRole = M('SysUserRole');
    
    if ( !is_array($role_id_arr) ) {
      $role_id_arr = explode(',', $role_id_arr);
    }
    
    $status = TRUE  ;
    foreach ( $role_id_arr as $role_id ) {
      $ret = $SysUserRole->add(array(
        'user_id' => $user_id ,
        'role_id' => $role_id ,
      ));
      if( !$ret ) {
        $status = FALSE ;
      }
    }
    
    if ( $status ) {
      return ajax_arr( '添加成功' , TRUE );
    } else {
      return ajax_arr( '添加失败' ) ;
    }
  }

  function update_user_role( $user_id , $role_id_arr ) {
    $change = FALSE ;
    if( !is_array($role_id_arr) ) {
      $role_id_arr = explode(',', $role_id_arr);
    }
    $SysUserRole = M('SysUserRole');
    
    $old_data = $SysUserRole->where('user_id = %d ' , $user_id )->select();
    foreach ( $old_data as $row ) {
      $search_key = array_search($row['role_id'], $role_id_arr);
      if ( !$search_key ) {
        //删除
        $SysUserRole->delete( $row['id'] );
        $change = TRUE ;
      } else {
        unset($role_id_arr[$search_key]);
      }
    }
    $new_arr = array();
    foreach ( $role_id_arr as $role_id ) {
      $new_arr[] = array(
        'user_id' => $user_id ,
        'role_id' => $role_id ,
      );
    }
    if ( !empty( $new_arr ) ) {
      $change = TRUE ;
      $SysUserRole->addAll($new_arr);
    }
    return $change ? ajax_arr('编辑成功', TRUE ) : ajax_arr('编辑失败');
  }
}
